package com.sls.game.service.impl;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.ServerDisponseException;
import com.sls.game.constant.ExceptionCodeDes;
import com.sls.game.dao.GoodsOrderLogDAO;
import com.sls.game.page.Pagination;
import com.sls.game.pojo.GoodsOrderLog;
import com.sls.game.querybean.GoodsOrderLogQuery;
import com.sls.game.service.GoodsOrderLogService;
import com.sls.game.socket.SuperService;

@Service("goodsOrderLogService")
public class GoodsOrderLogServiceImpl extends SuperService implements
		GoodsOrderLogService {

	private static Logger log = Logger
			.getLogger(GoodsOrderLogServiceImpl.class);
	@Resource(name = "goodsOrderLogDAO")
	private GoodsOrderLogDAO goodsOrderLogDAO;

	private Gson gson;

	public GoodsOrderLogServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public String queryOrderLogs(String body, Head head)
			throws ServerDisponseException {
		GoodsOrderLogQuery logQuery = gson.fromJson(body,
				GoodsOrderLogQuery.class);
		this.validate(logQuery);
		this.validatePageCondition(logQuery);
		try {
			Pagination<GoodsOrderLog> page = this.goodsOrderLogDAO
					.queryOrderLog(logQuery);
			if (page != null) {
				return gson.toJson(page);
			}
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		return null;
	}

	@Override
	public String queryOrderLogById(String body, Head head)
			throws ServerDisponseException {
		GoodsOrderLog orderLog = gson.fromJson(body, GoodsOrderLog.class);
		this.validate(orderLog);
		String id = orderLog.getId();
		if (StringUtils.isEmpty(id)) {
			throw new ServerDisponseException(
					ExceptionCodeDes.ID_EMPTY.getCode(),
					ExceptionCodeDes.ID_EMPTY.getDescription());
		}
		try {
			orderLog = this.goodsOrderLogDAO.queryById(id);
			if (orderLog != null) {
				return gson.toJson(orderLog);
			}
		} catch (Exception e) {
			log.error(e.toString());
			throw new ServerDisponseException(
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getCode(),
					ExceptionCodeDes.ACCESS_DATABASE_ERROR.getDescription());
		}
		return null;
	}

	/**
	 * 校验是否为null
	 * 
	 * @param obj
	 * @throws ServerDisponseException
	 */
	private void validate(Object obj) throws ServerDisponseException {
		if (obj == null) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getCode(),
					ExceptionCodeDes.PARSE_REQUEST_ERROR.getDescription());
		}
	}

	/**
	 * 校验分页必传参数是否为空
	 * 
	 * @param orderQuery
	 */
	private void validatePageCondition(GoodsOrderLogQuery logQuery) {
		String currentPage = String.valueOf(logQuery.getCurrentPage());
		String pageSize = String.valueOf(logQuery.getPageSize());
		if (StringUtils.isEmpty(currentPage)) {
			throw new ServerDisponseException(
					ExceptionCodeDes.CURRENT_PAGE_NULL.getCode(),
					ExceptionCodeDes.CURRENT_PAGE_NULL.getDescription());
		} else if (StringUtils.isEmpty(pageSize)) {
			throw new ServerDisponseException(
					ExceptionCodeDes.PAGE_SIZE_NULL.getCode(),
					ExceptionCodeDes.PAGE_SIZE_NULL.getDescription());
		} else {

		}
	}
}
